package edu.tjhsst.androidIntranet.news;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import edu.tjhsst.androidIntranet.R;
import edu.tjhsst.androidIntranet.db.NewsProvider;

public class NewsEntryPicker extends ListActivity {

	//private ArrayList<NewsEntry> mainData;
	//SimpleCursorAdapter mainAdapter;
	NewsAdapter mainAdapter;
	//NewsEntryListener nel;

	public void onCreate(Bundle savedInstanceState) {
		//Log.d("NewsEntryPicker", "Booting NewsEntryPicker");
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.news_viewer);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_update_title);
		((TextView)findViewById(R.id.title_left_text)).setText("TJ Intranet: News");
		
		//IntranetDbAdapter.NewsDbAdapter nDbA = IntranetDbAdapter.getNewsDbAdapter(this);
		//mainAdapter = new SimpleCursorAdapter(this, R.layout.news_content_row, nDbA.getNewsCursor(), new String[] {"date", "title", "content"}, new int[] {R.id.title, R.id.date, R.id.content});
		mainAdapter = new NewsAdapter(this);
		setListAdapter(mainAdapter);
		new NewsLoaderThread(new NewsHandler(), this).start();
		ListView lv = getListView();
        lv.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_INSET);
		//nel = new NewsEntryListener();
		//lv.setOnItemClickListener(nel);
	}
	
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Log.d("NewsEntryListener", "Item clicked, launching intent");
		NewsEntry newsEntry = (NewsEntry) l.getItemAtPosition(position);
		Uri launchUri = ContentUris.withAppendedId(Uri.parse("content://" + NewsProvider.NEWS_AUTHORITY + "/newsEntries"), (long)newsEntry.getIdentifier());
		Log.d("NewsEntryListener", "Launching Uri: " + launchUri.toString());
		startActivity(new Intent(Intent.ACTION_VIEW, launchUri));
		//NewsEntryPicker.this.startActivity(new Intent("android.intent.action.VIEW", new Uri.Builder().path(("content://tjIntranet/news/"+newsEntry.getIdentifier())).build(), NewsEntryPicker.this, NewsEntryPicker.class));
	}

	private class NewsHandler extends Handler {

		public void handleMessage(Message m) {
			/*if((Boolean)m.getData().get("IsUpdateComplete")) {

			} else {
				NewsEntry ne = (NewsEntry)m.getData().get("NewsEntry");
				for(int i=0; i<mainAdapter.getCount(); i++) {
					if(mainAdapter.getItem(i).getDatePosted().compareTo(ne.getDatePosted())>0) {
						mainAdapter.insert(ne);
					}
				}
			}*/
			if(m.getData().get("IsUpdateComplete")!=null) {
				TextView mRightText = (TextView) findViewById(R.id.title_right_text);
				ProgressBar mProgressIcon = (ProgressBar) findViewById(R.id.title_progress_icon);
				mRightText.setVisibility(View.INVISIBLE);
				mProgressIcon.setVisibility(View.INVISIBLE);
			} else {
				NewsEntry ne = (NewsEntry)m.getData().get("NewsEntry");
				mainAdapter.insert(ne.getIdentifier(), ne.getTitle(), ne.getContent(), ne.getPoster(), ne.getDatePosted(), ne.isRead());
				mainAdapter.updateList();
				NewsEntryPicker.this.onContentChanged();
				//NewsEntryPicker.this.getListView().setOnItemClickListener(nel);
			}
		}

	}

}
